package com.myphoto.business.photo.serviceimpl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.myphoto.base.config.properties.IConfig;
import com.myphoto.base.mybatis.photo.dao.VideoDao;
import com.myphoto.base.mybatis.photo.entity.VideoEntity;
import com.myphoto.business.photo.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Resource
    private VideoDao videoDao;
    @Autowired
    private IConfig config;

    @Override
    public List<VideoEntity> loadVideoList() throws Exception {
        LambdaQueryWrapper<VideoEntity> videoLambdaQueryWrapper = Wrappers.lambdaQuery();
        videoLambdaQueryWrapper.orderByDesc(VideoEntity::getVideoDate);
        return videoDao.selectList(videoLambdaQueryWrapper);
    }

    @Override
    @Transactional
    public void uploadVideo(String lastModified,MultipartFile file) throws Exception {
        VideoEntity videoEntity = new VideoEntity();
        videoEntity.setVideoName(file.getOriginalFilename());
        videoEntity.setVideoState("1");
        Date xgsj = DateUtil.parse(lastModified,"yyyy-MM-dd HH:mm:ss").toJdkDate();
        videoEntity.setVideoDate(xgsj);
        videoDao.insert(videoEntity);

        String video_path = config.getConfig().getVideo_path();//得到上传文件主路径
        String filePath = video_path + file.getOriginalFilename();//得到文件的存储路径
        File newFile = new File(filePath);
        // 检测是否存在目录
        if (!newFile.getParentFile().exists()) {
            //不存在则创建目录
            newFile.getParentFile().mkdirs();
        }
        // 使用 spring 的 transferTo() 方法写入
        file.transferTo(newFile);
    }

    @Override
    public boolean checkFileIsExist(String filename) throws Exception {
        String video_path = config.getConfig().getVideo_path();//得到上传文件主路径
        String filePath = video_path + filename;//得到文件的存储路径
        File newFile = new File(filePath);
        if(newFile.exists()){
            return true;
        }
        return false;
    }

    @Override
    public boolean checkFileInfoIsExist(String filename) throws Exception {
        LambdaQueryWrapper<VideoEntity> videoLambdaQueryWrapper = Wrappers.lambdaQuery();
        videoLambdaQueryWrapper.eq(VideoEntity::getVideoName,filename);
        videoLambdaQueryWrapper.eq(VideoEntity::getVideoState,"1");
        List list = videoDao.selectList(videoLambdaQueryWrapper);
        if(list!=null && list.size()>0){
            return true;
        }
        return false;
    }

    @Override
    @Transactional
    public void clearErrorVideoinfo() throws Exception {
        LambdaQueryWrapper<VideoEntity> videoLambdaQueryWrapper = Wrappers.lambdaQuery();
        List<VideoEntity> list = videoDao.selectList(videoLambdaQueryWrapper);
        if(list!=null && list.size()>0){
            String video_path = config.getConfig().getVideo_path();//得到上传文件主路径
            for(int i=0;i<list.size();i++){
                String picName = list.get(i).getVideoName();
                String filePath = video_path + picName;//得到文件的存储路径
                File newFile = new File(filePath);
                if(newFile.exists()==false){
                    videoDao.deleteById(list.get(i).getId());
                }
            }
        }
    }
}
